package com.tau.iplayer.client.dialogs;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.gadgets.client.gwtrpc.GadgetsGwtRpc;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.tau.iplayer.client.DataStoreService;
import com.tau.iplayer.client.DataStoreServiceAsync;
import com.tau.iplayer.client.IPlayer;
import com.tau.iplayer.client.models.Playlist;
import com.tau.iplayer.client.models.Track;

public class MoveTrackDialog extends DialogBox {

	private static final boolean IS_AUTO_HIDE = false;
	private static final boolean IS_MODAL = true;
	private static final boolean IS_ANIMATED = true;
	private static final boolean IS_GLASS = true;
	private static final String HEADER = "Move Track";
	private IPlayer iPlayer;
	private Track track;
	private ListBox playlistBox;

	public MoveTrackDialog(Track track, IPlayer iplayer) {
		setAutoHideEnabled(IS_AUTO_HIDE);
		setModal(IS_MODAL);
		setAnimationEnabled(IS_ANIMATED);
		setGlassEnabled(IS_GLASS);
		setText(HEADER);
		this.track = track;
		this.iPlayer = iplayer;
		createContents();
	}

	private void createContents() {
		HorizontalPanel horizontalPanel = new HorizontalPanel();
		final Button moveTrackButton = new Button("OK");
		moveTrackButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				handleMoveTrackClicked();
			}
		});
		moveTrackButton.setStyleName("button");

		Button cancelButton = new Button("Cancel");
		cancelButton.setStyleName("button");
		cancelButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				handleCancelClick();
			}
		});
		horizontalPanel.add(moveTrackButton);
		horizontalPanel.add(cancelButton);
		horizontalPanel.setSpacing(3);

		Grid formGrid = new Grid(1, 2);
		formGrid.setText(0, 0, "Playlist:");
		formGrid.getCellFormatter().setStyleName(0, 0, "standardLabel");
		playlistBox = new ListBox();
		for (Playlist playlist : iPlayer.getPlaylists()) {
			if (!playlist.equals(iPlayer.getCurrentPlaylist())) {
				playlistBox.addItem(playlist.getName());
			}
		}
		playlistBox.setStyleName("standardLabel");
		formGrid.setWidget(0, 1, playlistBox);

		VerticalPanel panel = new VerticalPanel();
		panel.add(formGrid);
		panel.add(horizontalPanel);
		panel.setCellHorizontalAlignment(horizontalPanel,
				HasHorizontalAlignment.ALIGN_CENTER);

		setWidget(panel);
	}

	private void handleCancelClick() {
		this.hide();
	}

	private void handleMoveTrackClicked() {
		DataStoreServiceAsync dataStoreService = GWT
		.create(DataStoreService.class);
		GadgetsGwtRpc.redirectThroughProxy((ServiceDefTarget) dataStoreService);
		Playlist newPlaylist = new Playlist(playlistBox.getValue(playlistBox.getSelectedIndex()));
		dataStoreService.moveTrack(iPlayer.getViewer().getId(), track, newPlaylist, new AsyncCallback<Void>() {
			
			@Override
			public void onSuccess(Void result) {
				MoveTrackDialog.this.hide();
				iPlayer.getCurrentPlaylist().loadTracks(iPlayer);
			}
			
			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}
		});
	}
}
